package qldc.pm.ksdl;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import qldc.pm.ksdl.action.DanhSachDanCuAction;
import qldc.pm.ksdl.action.ImportDanCuAction;
import qldc.pm.ksdl.action.ResourceActionHandler;
import qldc.pm.ksdl.util.KSDLConstants;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.Validator;

/**
 * The Class KiemSoatDuLieuPortlet.
 */
public class KiemSoatDuLieuPortlet extends BasePortlet {
	
	/** The _log. */
	private static Log _log = LogFactoryUtil.getLog(KiemSoatDuLieuPortlet.class);

	/** The resource actions. */
	private static Map<String, String> resourceActions;

	/* (non-Javadoc)
	 * @see com.liferay.util.bridges.mvc.MVCPortlet#init()
	 */
	@Override
	public void init() throws PortletException {
		resourceActions = new HashMap<String, String>();
		resourceActions.put(KSDLConstants.DANH_SACH_DAN_CU, DanhSachDanCuAction.class.getName());
		resourceActions.put(KSDLConstants.IMPORT_DAN_CU_90, ImportDanCuAction.class.getName());
		
		super.init();
	}

	/* (non-Javadoc)
	 * @see qldc.pm.qlnncdk.BasePortlet#getResourceActions()
	 */
	@Override
	public Map<String, String> getResourceActions() {
		return resourceActions;
	}

	/* (non-Javadoc)
	 * @see qldc.pm.qlnncdk.BasePortlet#defaultProcess(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
	 */
	@Override
	public void defaultProcess(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see qldc.pm.qlnncdk.BasePortlet#defaultProcess(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
	 */
	@Override
	public void defaultProcess(ActionRequest actionRequest,
			ActionResponse actionResponsee) throws IOException,
			PortletException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see qldc.pm.qlnncdk.BasePortlet#defaultProcess(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
	 */
	@Override
	public void defaultProcess(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		// TODO Auto-generated method stub

	}

	// Serve AJAX request
	/* (non-Javadoc)
	 * @see qldc.pm.qlnncdk.BasePortlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
	 */
	@Override
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		// get resourceID
		String resourceID = resourceRequest.getResourceID();

		if(Validator.isNotNull(resourceID)) {
			ResourceActionHandler resourceActionHandler = new ResourceActionHandler();

			try {
				Method method = ResourceActionHandler.class.getMethod(resourceID, ResourceRequest.class, ResourceResponse.class);

				if(Validator.isNotNull(method)) {
					method.invoke(resourceActionHandler, resourceRequest, resourceResponse);
				}
			} catch (NoSuchMethodException e) {
				_log.error(e);
			} catch (SecurityException e) {
				_log.error(e);
			} catch (IllegalAccessException e) {
				_log.error(e);
			} catch (IllegalArgumentException e) {
				_log.error(e);
			} catch (InvocationTargetException e) {
				_log.error(e);
			}
		}
	}
}
